package 练习;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright (C) 2023 思创数码科技股份有限公司
 * <p/>
 * 版权所有
 * <p/>
 * 类名　　  : Test1
 * 功能概要  :
 *
 * @author yangwk
 * @date 2024/5/31 21:41
 */
public class Test1 {
    public static Integer max(String str) {
        String maxStr = "";
        char[] arr = str.toCharArray();
        StringBuilder num = new StringBuilder();
        out:
        for (int i = 0; i < arr.length; i++) {
            if (isNotValid(arr[i])) {
                if (num.length() > maxStr.length()) {
                    maxStr = num.toString();
                }
                num = new StringBuilder();
            }
            while (isDigit(arr[i])) {
                num.append(arr[i]);
                continue out;
            }
            if (i < arr.length - 1 && isOperator(arr[i]) && isDigit(arr[i + 1])) {
                num.append(arr[i]);
                continue out;
            }else{
                if (num.length() > maxStr.length()) {
                    maxStr = num.toString();
                }
                num = new StringBuilder();
            }


        }
        if (num.length() > maxStr.length()) {
            maxStr = num.toString();
        }
        System.out.println(maxStr);
        return convertCal(maxStr);
    }

    public static Integer convertCal(String operate) {
        char[] chars = operate.toCharArray();
        List<String> operator = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            if (isOperator(chars[i])) {
                if (stringBuilder.length() != 0) {
                    operator.add(stringBuilder.toString());
                }
                stringBuilder = new StringBuilder();
            }
            stringBuilder.append(chars[i]);
        }
        if (stringBuilder.length() != 0) {
            operator.add(stringBuilder.toString());
        }
        return val(operator);
    }

    public static Integer val(List<String> operate) {
        Integer val = 0;
        for (int i = 1; i < operate.size(); ) {
            int i1 = Integer.parseInt(operate.get(i));
            int i2 = Integer.parseInt(operate.get(i - 1));
            int i3 = i1 + i2;
            operate.add(String.valueOf(i3));
            operate.remove(0);
            operate.remove(0);
        }
        return Integer.parseInt(operate.get(0));
    }

    public static boolean isOperator(char ch) {
        return '-' == ch || '+' == ch;
    }

    public static boolean isNotValid(char ch) {
        return !isOperator(ch) && !isDigit(ch);
    }

    public static boolean isDigit(char ch) {
        return '0' <= ch && ch <= '9';
    }

    public static void main(String[] args) {
        System.out.println(max("1-2-1-2--1-+2-1-1-11-2-2-"));

    }
}
